package cn.aitrox.ry.service.anno.mq.consumer;

import cn.aitrox.ry.constant.CommonConstant;
import cn.aitrox.ry.service.anno.mq.msg.FileHandlerMsg;
import cn.aitrox.ry.service.anno.service.FileHandlerInfoService;
import cn.aitrox.ry.util.JSONUtil;
import cn.aitrox.ry.util.LogUtil;
import org.apache.activemq.command.ActiveMQTextMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.jms.Message;
import javax.jms.MessageListener;

@Component
public class FileHandlerConsumer implements MessageListener {

    private static final Logger LOG = LoggerFactory.getLogger(FileHandlerConsumer.class);

    @Autowired
    private FileHandlerInfoService fileHandlerInfoService;


    @Override
    public void onMessage(Message message) {
        String msg = CommonConstant.EMPTY;
        try {
            ActiveMQTextMessage textMessage = (ActiveMQTextMessage) message;
            msg = textMessage.getText();
            LOG.info(LogUtil.gen("FileHandlerConsumer receive", "msg", msg));
            FileHandlerMsg fileHandlerMsg = JSONUtil.fromJson(msg, FileHandlerMsg.class);

            fileHandlerInfoService.handlerMsg(fileHandlerMsg);

            LOG.info(LogUtil.gen("FileHandlerConsumer success", "msg", msg));
        } catch (Exception e) {
            LOG.info(LogUtil.genException(e, "FileHandlerConsumer error", "msg", msg), e);
        }
    }
}
